package com.cnbot.canapp.listener.battery;

import android.util.Log;

import com.cnbot.canapp.listener.base.MultiSerialItem;
import com.cnbot.canapp.utils.ICanCode;
import com.dc.android_can.can.CanDataType;

/**
 * 描述：环境
 * 作者：dc on 2018/6/20 14:09
 * 邮箱：597210600@qq.com
 */
public class BatteryItem extends MultiSerialItem<BatteryImp> {

    public BatteryItem(BatteryImp batteryImp){
        super(batteryImp, ICanCode.BUF_BATTERY_GROUP_CODE, ICanCode.BUF_RESIDUAL_ELE_CODE, ICanCode.BUF_BATTERY_CHANGE_STATUS_CODE,
              ICanCode.BUF_BATTERY_PROTECT_STATUS_CODE, ICanCode.BUF_BATTERY_CURRENT_STATUS_CODE, ICanCode.BUF_BATTERY_ELE_CODE);
    }
    @Override
    public void onOrderDispatch(int id) {
        if(id == ICanCode.BUF_BATTERY_CODE && !canDataType.dir){
//            Log.e(TAG, "电池");
            batteryCanRead(canDataType);
        }
    }

    /**
     * @descriptoin	环境板数据
     * @author	dc
     * @param canDataType 接收到的数据结构体
     * @date 2018/6/13 9:18
     */
    private void batteryCanRead(CanDataType canDataType){
        // 电池板
        if(canDataType.cmdWord == ICanCode.BUF_RESIDUAL_ELE_CODE){
            // 剩余电量
            double residualEle = (canDataType.buf[1] * 256) + canDataType.buf[0] ;
            Log.e(TAG, "剩余电量  = " + residualEle);
            mListener.robotResidualEle(residualEle + "");
        } else if(canDataType.cmdWord == ICanCode.BUF_BATTERY_CHANGE_STATUS_CODE){
            // 读取电池充电状态
            int status = (canDataType.buf[0]);
            Log.e(TAG, "充电状态  = " + status);
            mListener.robotBatteryChangeStatus(status);
        } else if(canDataType.cmdWord == ICanCode.BUF_BATTERY_PROTECT_STATUS_CODE){
            // 读取电池保护状态
            int status = (canDataType.buf[1] * 256) ;
            Log.e(TAG, "读取电池保护状态  = " + status);
            mListener.robotProtectStatus(status);
        }
    }

}
